/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ligacaoBD.BDconnection;
import logica.Loja;

/**
 *
 * @author JoséRego
 */
public class pesquisaLojas extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String query = request.getParameter("query");

        try {
            String querySQL = "";
            BDconnection bd = new BDconnection();
            if (query.equals("")) {
                querySQL = "Select * from Loja";
            } else {
                querySQL = "Select * from Loja where Nome LIKE '%" + query + "%' Or Rua LIKE '%" + query + "%' OR Localidade LIKE '%" + query + "%'"
                        + "OR Pais LIKE '%" + query + "%'";
            }
            ResultSet rs = bd.runQuery(querySQL);
            ArrayList<Loja> lojas = new ArrayList<Loja>();
            while (rs.next()) {
                Loja loja = new Loja(rs.getString("Nome"), Integer.parseInt(rs.getString("IdLoja")), Double.parseDouble(rs.getString("coordx")),
                        Double.parseDouble(rs.getString("coordy")), rs.getString("Rua"), rs.getString("Localidade"), rs.getString("Pais"));
                lojas.add(loja);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lojas.size(); i++) {
                sb.append("<tr id=\"").append(lojas.get(i).getIdLoja() + "\"").append(" onclick=\"addMaker(").append(lojas.get(i).getLat()).append(",").append(lojas.get(i).getLng()).append(")\" onmouseover=\"this.style.background= 'LightGrey';\" onmouseout=\"this.style.background='white';\">\n"
                        + "<td><h3>").append(lojas.get(i).getNome()).append("</h3>\n"
                        + "" + lojas.get(i).getRua() + ", " + lojas.get(i).getLocalidade() + " - " + lojas.get(i).getPais() + "\n"
                        + "</td>\n"
                        + "\n"
                        + " </tr>");

            }
            out.write(sb.toString());
        } finally {

            out.flush();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(pesquisaLojas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(pesquisaLojas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
